package com.topscomm.mpm.xxlJob;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.topscomm.basic.BasicEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author houyuhang
 * @date 2022/12/13
 * @description: 项目评分细则完成情况
 * @modify:
 */
@JobHandler(value = "allBidScoreInfo")
@Component
public class AllBidScoreInfo extends IJobHandler {
    @Autowired
    public MpmProjectService mpmProjectService;
    @Override
    public ReturnT<String> execute(String param) {
        try {
            StringBufferProxy sql = new StringBufferProxy();
            sql.appendSingle(" state = 9 ");
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("whereSql", sql.toString());
            List<Map<String, Object>> mpmProjectList = mpmProjectService.queryMap(dataMap, "queryPolymarizationMapByWhere");
            List<Map<String, Object>> basicMap = new ArrayList<>();
            for(Map<String, Object> mpmProject : mpmProjectList) {
                this.firstBidScoreState(mpmProject);
                this.secondBidScoreState(mpmProject);
                Map<String, Object> tempMap = new HashMap<>();
                tempMap.put("id", mpmProject.get("id"));
                tempMap.put("firstbidscore", mpmProject.get("firstbidscore"));
                tempMap.put("secondbidscore", mpmProject.get("secondbidscore"));
                basicMap.add(mpmProject);
            }
            mpmProjectService.updateBatchMap(basicMap);
            return ReturnT.SUCCESS;
        } catch (Exception e) {
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        }
    }
    /**
     * @author houyuhang
     * @date 2022/12/13
     * @description: 项目第一次评分细则状态
     * @modify:
     */
    public void firstBidScoreState(Map<String, Object> mpmProject) {
        // 项目已有第一次、第二次自评信息，不同步评分细则状态
        String firststate = ConvertUtil.convertToString(mpmProject.get("firstbidscore"));
        if (firststate.length() > 1) {
            return;
        }
        String purchasepattern = ConvertUtil.convertToString(mpmProject.get("purchasepattern"));
        // 非投标项目，显示“/”
        if (MpmSystemConst.PurchasePattern.noBid.equals(purchasepattern) || MpmSystemConst.PurchasePattern.otherNoBid.equals(purchasepattern)) {
            mpmProject.put("firstbidscore", MpmSystemConst.ProjectBidScoreState.nobid);
            return;
        }
        if (ObjectUtil.isEmpty(mpmProject.get("bidbegindate"))) {
            return;
        }
        // 超过挂网日期
        if (new Date().compareTo(ConvertUtil.convertToDate(mpmProject.get("bidbegindate"))) > 0) {
            mpmProject.put("firstbidscore", MpmSystemConst.ProjectBidScoreState.nowrite);
            return;
        }
        // 策划审批通过后，挂网日期之前，未录入第一次自评，显示超期，否则显示待录入
        if (!ObjectUtil.isEmpty(mpmProject.get("designenddate"))) {
            mpmProject.put("firstbidscore", MpmSystemConst.ProjectBidScoreState.overdue);
            return;
        } else {
            mpmProject.put("firstbidscore", MpmSystemConst.ProjectBidScoreState.waitwrite);
            return;
        }
    }
    /**
     * @author houyuhang
     * @date 2022/12/13
     * @description: 项目第二次评分细则状态
     * @modify:
     */
    public void secondBidScoreState(Map<String, Object> mpmProject) {
        String secondstate = ConvertUtil.convertToString(mpmProject.get("secondbidscore"));
        if (secondstate.length() > 1) {
            return;
        }
        String purchasepattern = ConvertUtil.convertToString(mpmProject.get("purchasepattern"));
        // 非投标项目，显示“/”
        if (MpmSystemConst.PurchasePattern.noBid.equals(purchasepattern) || MpmSystemConst.PurchasePattern.otherNoBid.equals(purchasepattern)) {
            mpmProject.put("secondbidscore", MpmSystemConst.ProjectBidScoreState.nobid);
            return;
        }
        if (ObjectUtil.isEmpty(mpmProject.get("bidbegindate")) || ObjectUtil.isEmpty(mpmProject.get("bidenddate"))) {
            return;
        }
        // 超过开标日期
        if (new Date().compareTo(ConvertUtil.convertToDate(mpmProject.get("bidenddate"))) > 0) {
            mpmProject.put("secondbidscore", MpmSystemConst.ProjectBidScoreState.nowrite);
            return;
        }
        // 挂网日期之前，显示未挂网;挂网日期及挂网日期7天内，未录入第二次自评，显示“待录入”;挂网日期的7天后，开标日期之间，未录入第二次自评，显示“超期”。
        if (new Date().compareTo(ConvertUtil.convertToDate(mpmProject.get("bidbegindate"))) > 0) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -7);
            if (calendar.getTime().compareTo(ConvertUtil.convertToDate(mpmProject.get("bidbegindate"))) > 0) {
                mpmProject.put("secondbidscore", MpmSystemConst.ProjectBidScoreState.overdue);
                return;
            } else {
                mpmProject.put("secondbidscore", MpmSystemConst.ProjectBidScoreState.waitwrite);
                return;
            }
        } else {
            mpmProject.put("secondbidscore", MpmSystemConst.ProjectBidScoreState.nobegindate);
            return;
        }
    }
}
